package org.flower.dao.impl;

import org.flower.dao.CommodityshowDao;
import org.flower.entity.CommodityDetails;
import org.flower.entity.Commodityshow;
import org.flower.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品展示实现类
 */
public class CommodityshowDaoImpl extends BaseDao implements CommodityshowDao {
    @Override
    public List<Commodityshow> getcomdShow(int floorId) {
        List<Commodityshow>  comlist = new ArrayList<Commodityshow>();
        String sql = "select c.*,d.comImage,d.comName,d.outPrice,d.outNum,d.id from Commodityshow c inner join commoditydetails d on c.comid=d.id where floorId =?";
        ResultSet rs = super.executeQuery(sql,floorId);
        try {
            while (rs.next()){
                Commodityshow comds = new Commodityshow();
                comds.setId(rs.getInt("c.id"));
                comds.setFloorId(rs.getInt("floorId"));
                comds.setCommId(rs.getInt("d.id"));
                //创建商品详情对象
                CommodityDetails comdd = new CommodityDetails();
                comdd.setId(rs.getInt("d.id"));
                comdd.setComName(rs.getString("d.comName"));
                String imageStr = rs.getString("d.comImage").split(",")[0];
                comdd.setComImage("image/commodity/"+imageStr);
                comdd.setOutPrice(rs.getDouble("d.outPrice"));
                comdd.setOutNum(rs.getInt("d.outNum"));
                //将商品详情信息添加到商品展示信息中
                comds.setComd(comdd);
                comlist.add(comds);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return comlist;
    }
}
